package uk.ac.cam.tcs40.sidnet.formal;

import java.util.Calendar;

import uk.ac.cam.tcs40.sidnet.Hall;
import uk.ac.cam.tcs40.sidnet.MainActivity;
import uk.ac.cam.tcs40.sidnet.R;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Events;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class FormalActivity extends FragmentActivity implements ActionBar.TabListener {

	private Formal m_Formal;

	private FormalPagerAdapter m_FormalPagerAdapter;
	private ViewPager m_ViewPager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		// Set up the action bar.
		final ActionBar actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		// Create the adapter that will return a fragment for each of the primary sections of the app.
		m_FormalPagerAdapter = new FormalPagerAdapter(getSupportFragmentManager());

		// Set up the ViewPager with the sections adapter.
		m_ViewPager = (ViewPager) findViewById(R.id.pager);
		m_ViewPager.setAdapter(m_FormalPagerAdapter);

		// When swiping between different sections, select the corresponding
		// tab. We can also use ActionBar.Tab#select() to do this if we have
		// a reference to the Tab.
		m_ViewPager
		.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				actionBar.setSelectedNavigationItem(position);
			}
		});

		// For each of the sections in the app, add a tab to the action bar.
		for (int i = 0; i < m_FormalPagerAdapter.getCount(); i++) {
			// Create a tab with text corresponding to the page title defined by
			// the adapter. Also specify this Activity object, which implements
			// the TabListener interface, as the callback (listener) for when
			// this tab is selected.
			actionBar.addTab(actionBar.newTab()
					.setText(m_FormalPagerAdapter.getPageTitle(i))
					.setTabListener(this));
		}

		getActionBar().setDisplayHomeAsUpEnabled(true);

		Bundle extras = getIntent().getExtras();

		String id;
		if (extras != null) {
			id = extras.getString("id");
			if (id != null) {
				m_Formal = Hall.getInstance().getFormal(id);
			}
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case android.R.id.home:
			// This is called when the Home (Up) button is pressed
			// in the Action Bar.
			Intent parentActivityIntent = new Intent(this, MainActivity.class);
			parentActivityIntent.putExtra("formal",  true);
			parentActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(parentActivityIntent);
			finish();
			return true;

		case R.id.action_book_formal:
			new BookFormalTask().execute();
			break;

		case R.id.action_add_event:
			Intent intent = new Intent(Intent.ACTION_INSERT);
			intent.setType("vnd.android.cursor.item/event");

			intent.putExtra(Events.TITLE, "Formal");
			intent.putExtra(Events.EVENT_LOCATION, "Sidney Sussex College, Cambridge");

			Calendar start = Calendar.getInstance();
			start.setTime(m_Formal.getDate());
			start.set(Calendar.HOUR_OF_DAY, 19);
			start.set(Calendar.MINUTE, 30);

			intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, start.getTimeInMillis());

			Calendar end = Calendar.getInstance();
			end.setTime(m_Formal.getDate());
			end.set(Calendar.HOUR_OF_DAY, 21);

			intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, end.getTimeInMillis());

			startActivity(intent);
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.activity_formal, menu);
		return true;
	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		m_ViewPager.setCurrentItem(tab.getPosition());
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
	}

	private class BookFormalTask extends AsyncTask<Void, Void, Boolean> {
		protected void onPreExecute() {
		}

		protected Boolean doInBackground(Void... v) {
			boolean booked = Hall.getInstance().bookFormal(m_Formal);
			Hall.getInstance().getFormalSidnetIDList(m_Formal);
			return booked;
		}

		protected void onProgressUpdate(Void... progress) {
		}

		protected void onPostExecute(Boolean result) {
		}
	}

}
